class OrdersController < ApplicationController
  before_filter :login_required
  before_filter :check_merchandiser_right,:except=>[:show,:new,:create]
  before_filter :find_user,:only=>[:index,:new,:create,:edit]
  # GET /orders
  # GET /orders.xml
  def index
    if merchandiser?
      @orders = Order.paginate :page => params[:page],:per_page => 10,:order=>'id desc'
    else
      @orders = @user.orders.paginatae :page => params[:page],:per_page => 10,:order=>'id desc'
    end
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @orders }
    end
  end

  # GET /orders/1
  # GET /orders/1.xml
  def show
    @order = Order.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @order.products.to_xml(:skip_types => true,:dasherize => false,
          :only=>[:id,:style_id,:fm,:bm,:chest,:frd,:fld,:brd,:name,:price])}
    end
  end

  # GET /orders/new
  # GET /orders/new.xml
  def new
    @order = Order.new
    @order_owen = @user
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @order }
    end
  end

  # GET /orders/1/edit
  def edit
    @order = Order.find(params[:id])
    @order_owen = @order.user
  end

  # POST /orders
  # POST /orders.xml
  def create
    @order = @user.orders.build(params[:order])
    @order.add_line_items_from_cart(@cart)

    if session[:agent_id]
      agent=User.find(session[:agent_id])
      @order.agent_id = session[:agent_id]
      @order.discount = agent.discount
    end

    if params[:address]
      address = @user.addresses.build(params[:address])
      @order.address = address if address.save
    end

    Order.transaction do
      @order.save
      @order.lock_product
    end

    if @order.id
      session[:cart] = nil
      if @order.paytype == Order::ALIPAY
        redirect_to :action =>:new,:controller=>:payments,:id=>@order.id
      else
        redirect_to :action =>:order_success_tips,:controller=>:store,:id=>@order.id
      end
    else
      respond_to do |format|
        format.html { render :action => "new" }
        format.xml  { render :xml => @order.errors, :status => :unprocessable_entity }
      end
    end
    
  end

  # PUT /orders/1
  # PUT /orders/1.xml
  def update
    @order = Order.find(params[:id])

    respond_to do |format|
      if @order.update_attributes(params[:order])
        flash[:notice] = '订单更新成功.'
        format.html { redirect_to('/store/mine') }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @order.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /orders/1
  # DELETE /orders/1.xml
  def destroy
    @order = Order.find(params[:id])
    Order.transaction do
      @order.gc_product
      @order.destroy
    end
   
    respond_to do |format|
      format.html { redirect_to('/store/mine') }
      format.xml  { head :ok }
    end
  end

  def pay
    if merchandiser?
      @order = Order.find(params[:id])
      if params[:pay_status] == "0"
        @order.pay_time = nil
      elsif params[:pay_status] == "1"
        @order.pay_time = Time.now
      end
      @order.pay_remark = params[:order][:pay_remark]

      respond_to do |format|
        if @order.save
          flash[:notice] = '订单支付状态更新成功'
          format.html { redirect_to(:controller=>'store',:action=>"mine") }
        else
          format.html { render :action => "index" }
        end
      end
    end
  end

end
